<?php

namespace app\admin\controller;

use think\Request;
use app\admin\model\AdminMenuModel;
use think\Validate;
use think\facade\Cache;

class AdminMenu extends Base
{
    public function initialize()
    {
        parent::initialize();
    }

    //后台菜单管理
    public function index()
    {
        $admin_menu_model = new AdminMenuModel;
        $menu_tree = $admin_menu_model->getMenuTree();
        return $this->fetch('', [
            'data' => $menu_tree,
        ]);
    }

    /**
     * 添加菜单
     * @param Request $request
     * @return mixed|\think\response\Json
     */
    public function menuAdd(Request $request)
    {
        $admin_menu_model = new AdminMenuModel;
        if ($request->isAjax()) {
            $validate = new Validate([
                'name|菜单名称' => 'require|length:1,10|token',
            ]);
            if ($validate->check($request->param())) {
                $res = $admin_menu_model->menuAdd($request->param());
                if ($res['status'] == 'n') {
                    $res['token'] = $request->token();
                }
                return json($res);
            } else {
                return json(['status' => 'n', 'info' => $validate->getError(), 'token' => $request->token()]);
            }
        }
        $menu_tree = $admin_menu_model->getMenuTree();
        return $this->fetch('', [
            'menu_tree' => $menu_tree,
            'menu_id' => $request->param('menu_id') ? $request->param('menu_id') : 0
        ]);
    }

    public function menuEdit(Request $request)
    {
        $admin_menu_model = new AdminMenuModel;
        if ($request->isAjax()) {
            $validate = new Validate([
                'name|菜单名称' => 'require|length:1,10|token',
            ]);
            if ($validate->check($request->param())) {
                $res = $admin_menu_model->menuEdit($request->param());
                if ($res['status'] == 'n') {
                    $res['token'] = $request->token();
                }
                return json($res);
            } else {
                return json(['status' => 'n', 'info' => $validate->getError(), 'token' => $request->token()]);
            }
        }
        $res = $admin_menu_model->getMenu($request->param('id'));
        if (empty($res)) {
            $this->error('菜单数据不存在', url('admin/menu/index'));
        }
        $menu_tree = $admin_menu_model->getMenuTree();
        return $this->fetch('', [
            'menu_tree' => $menu_tree,
            'data' => $res
        ]);
    }

    /**
     * 删除菜单
     * @param Request $request
     * @return \think\response\Json
     */
    public function menuDel(Request $request)
    {
        $admin_menu_model = new AdminMenuModel;
        $id = $request->param('id');
        //获取菜单数据
        $menu = $admin_menu_model->getMenu($id);
        if (empty($menu)) {
            return json(['status' => 'n', 'info' => '菜单数据不存在']);
        }
        $res = $admin_menu_model->where('id', $id)->delete();
        if ($res) {
            Cache::clear();
            return json(['status' => 'y', 'info' => '删除成功！']);
        } else {
            return json(['status' => 'n', 'info' => '删除失败！']);
        }
    }

    public function menuOrder(Request $request)
    {
        $admin_menu_model = new AdminMenuModel;

        //处理排序数据
        $update_data = [];
        $order_str = $request->param('order_str', '', null);
        $order_arr = explode('&', $order_str);
        foreach ($order_arr as $v) {
            $id_order = explode('=', $v);
            $update_data[] = ['id' => $id_order[0], 'order' => $id_order[1]];
        }
        if (false !== $admin_menu_model->saveAll($update_data)) {
            return json(['status' => 'y', 'info' => '排序成功！']);
        } else {
            return json(['status' => 'n', 'info' => '排序失败！']);
        }
    }
}
